/**
 * @file 照片配置
 * @author 李刚
 * @date 2021-09-25 15:24:11
 */
var channelListTableData = new Array();
layui.define(['element', 'layer', 'table', 'upload', 'form', '$http', 'previewer'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let $http = layui.$http;
    let layer = layui.layer;
    let table = layui.table;


    channelListTableData = getListTable()
    let col_channel = [
        [
            {
                field: 'num', title: '序号', width: 80, templet(row) {
                    return row.LAY_INDEX
                }
            },
            {field: 'channelId', title: '渠道ID', align: 'center', unresize: true, sort: false},
            {field: 'channelName', title: '渠道名称', align: 'center', unresize: true, sort: false},
            {
                field: 'right', fixed: "right", width: 90, title: '操作', templet: function (row) {
                    return '<a class="layui-btn layui-btn-danger layui-btn-xs channelDelete"  lay-event="del">删除</a>';
                }
            }
        ]
    ];

    let tableChannel = table.render({
        elem: "#zmn-prom-modify-channel-table",
        id: "zmn-prom-modify-channel-table",
        toolbar: "#zmn-prom-modify-channel-table-tool",
        title: "分类列表",
        cols: col_channel,
        page: {curr: 1}, //是否开启分页
        loading: true,
        height: 'full-100',
        defaultToolbar: ['filter'],
        data: channelListTableData
    });

    form.on('submit(zmn-form-channel-query-submit)', function (obj) {
        let data = getListTable()
        let keyWord = obj.field.keyWord
        if (!keyWord) {
            tableChannel.reload({
                data: data
            });
            return false;
        }
        data = data.filter(item => {
            return item.channelId.toString().indexOf(keyWord) !== -1 || item.channelName.indexOf(keyWord) !== -1
        });
        tableChannel.reload({
            page: {curr: 1}, //重新从第 1 页开始
            data: data
        });
        return false;
    });

    let timer = setInterval(function () {
        let json = window.sessionStorage.getItem("cooperation_checked_row")
        if (json) {
            let data = JSON.parse(json)
            for (let i = 0; i < data.length; i++) {
                channelListTableData.push(data[i])
            }
            window.sessionStorage.setItem("list_channel_table_data", JSON.stringify(channelListTableData))
            tableChannel.reload({
                page: {curr: 1}, //重新从第 1 页开始
                loading: true,
                data: channelListTableData
            });
            window.sessionStorage.removeItem("cooperation_checked_row")
        }
        let switch_allChannel = window.sessionStorage.getItem("switch_allChannel")
        if (switch_allChannel == 1) {
            tableChannel.reload({
                page: {curr: 1}, //重新从第 1 页开始
                loading: true,
                data: []
            });
            window.sessionStorage.removeItem("switch_allChannel")
        }
        if (switch_allChannel == 2) {
            tableChannel.reload({
                page: {curr: 1}, //重新从第 1 页开始
                loading: true,
                data: channelListTableData
            });
            window.sessionStorage.removeItem("switch_allChannel")
        }
    }, 200)

    table.on('tool(zmn-prom-modify-channel-table)', function (obj) {
        if (obj.event === "del") {
            layer.confirm("你确定要删除么？", {btn: ['是的,我确定', '我再想想']},
                function () {
                    let oldData = table.cache["zmn-prom-modify-channel-table"];
                    console.log("obj.tr.data('index')", obj.tr.data('index'))
                    let channel = oldData[obj.tr.data('index')]
                    layer.msg("删除成功", {time: 1}, function () {
                        channelListTableData = channelListTableData.filter(item => {
                            return item.channelId !== channel.channelId
                        });
                        window.sessionStorage.setItem("list_channel_table_data", JSON.stringify(channelListTableData))
                        tableChannel.reload({
                            page: {curr: 1}, //重新从第 1 页开始
                            loading: true,
                            data: channelListTableData
                        });
                    });
                })
        }
    })

    let initMethod = {
        tableChannel: function () {
            return tableChannel;
        },
    }

    function getListTable() {
        let json = window.sessionStorage.getItem("list_channel_table_data")
        channelListTableData = JSON.parse(json) || []
        return channelListTableData;
    }

    exports('oms_conf_attachment_orderPicture_channel', initMethod);
})
;
